﻿@page
@{
    ViewData["Title"] = "角色修改/添加";
    Layout = "~/Pages/Admin/_Layout.cshtml";
}

<div id="app">
    <form class="layui-form form-cus" action="" lay-filter="forms">

        <div class="layui-form-item">
            <label class="layui-form-label">消息类型</label>
            <div class="layui-input-block">
                <div id="dicType" name="Type" class="xm-select-demo"></div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">收件人</label>
            <div class="layui-input-block">
                <div id="roles" class="xm-select-demo"></div>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea v-model="m.content" style="min-height: 100px;" name="Summary"
                          class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item layui-cur-submit">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="submit" id="submit">
                    <i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop layui-hide"></i>提交
                </button>
                <button type="button" class="layui-btn layui-btn-primary btn-open-close">取消</button>
            </div>
        </div>
    </form>
</div>

<script>
    var vm = new Vue({
        el: '#app',
        data: {
            m: {}
        }
    });
        layui.use(['layer', 'jquery', 'common', 'form'], function () {
            var form = layui.form, $ = layui.$, os = layui.common;


            var index = parent.layer.getFrameIndex(window.name);

            //下拉框
            var dicTypeSelect = os.selectSingleRender({
                el: '#dicType',
                tips: '请选择消息类型',
                data: [
                    { name: '通知', id: 0 },//这里的id是value  标准的其实是name value  请看common.js
                    { name: '私信', id: 1 },
                    { name: '待办事务', id: 2 },
                ]
            });

            //下拉框
            var accountSelect = os.selectRender({
                el: '#roles',
                tips: '请选择收件人',
                 prop: {
                    name: 'trueName',
                    value: 'id',
                 },
                 toolbar: {
                    show: true,
                },
                filterable: true,
                paging: true,
                pageSize: 3
                //data: [
                //    {
                //        name: '销售员', children: [
                //            { name: '张三1', value: 1 },
                //            { name: '李四1', value: 2 },
                //            { name: '王五13', value: 3 },
                //        ]
                //    },
                //    {
                //        name: '奖品', children: [
                //            { name: '苹果23', value: 4 },
                //            { name: '香蕉2', value: 5 },
                //            { name: '葡萄2', value: 6 },
                //        ]
                //    },
                //]
            });



            oc = {
                //初始化
                init() {
                    
                        os.ajax('api/admin/sysuser/list', { size: 10000 }, function (res) {
                            //加载所有账户
                            accountSelect.update({
                               data: res.data.rows
                            })
                        }, 'get');
                }

            };



            oc.init();

            //监听提交
            form.on('submit(submit)', function (data) {
                $('#submit').attr('disabled', true).find('i').removeClass('layui-hide');
                var urls = "api/admin/sysmessage/add";
                //获取选中的值
                if (dicTypeSelect.getValue().length > 0) {
                    vm.m.type = dicTypeSelect.getValue()[0].id;
                }

                var str = '';
                $.each(accountSelect.getValue(), function (i, item) {
                    str += item.id + ",";
                });
                str = str.substring(0, str.length - 1);

                vm.m.toUserId = str;
                console.log(vm.m.toUserId);
                os.ajax(urls, vm.m, function (res) {
                    $('#submit').attr('disabled', false).find('i').addClass('layui-hide');
                    if (res.code == 1) {
                        parent.layer.close(index);
                        //刷新父页面的数据
                        $('button[data-type="reload"]', window.parent.document).click();
                    }
                    else {
                        os.error(res.msg);
                    }
                }, 'post');

                return false;
            });
            $(".btn-open-close").on('click', function () {
                parent.layer.close(index);
            });
       




    });
</script>
